﻿using System;
using System.Text.RegularExpressions;
using System.Text;

namespace KKCai.Utils
{
    public class StringHelper
    {
        private const string RANDOM_STRING = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
        
        public static string RandomCode(int length)
        {
            try
            {
                string code = string.Empty;
                Random rand = new Random();
                for (int i = 0; i < length; i++)
                {
                    code += RANDOM_STRING.Substring(rand.Next(44), 1);
                }

                return code;
            }
            catch { return string.Empty; }
        }

        public static string CutLen(string value, int length)
        {
            if (string.IsNullOrEmpty(value))
                return string.Empty;

            return value.Length > length ? value.Substring(0, length) : value;
        }

        public static string NoHTML(string value)
        {
            if (String.IsNullOrEmpty(value))
            {
                return String.Empty;
            }

            //删除脚本  
            value = Regex.Replace(value, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML  
            value = Regex.Replace(value, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"-->", "", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"<!--.*", "", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&rdquo;", "", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&ldquo;", "", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&mdash;", "", RegexOptions.IgnoreCase);
            value = Regex.Replace(value, @"&middot;", "", RegexOptions.IgnoreCase);
            value.Replace("<", "");
            value.Replace(">", "");
            value.Replace("\r\n", "");
            //Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

            return value;
        }

        /// <summary>
        /// 定义正则表达式用来匹配 img 标签
        /// </summary>
        private static readonly Regex Regex_Image = new Regex(
            @"<img\b[^>]*?\bsrc[\s]*=[\s]*[""']?[\s]*(?<imgUrl>[^""'>]*)[^>]*?/?[\s]*>",
            RegexOptions.IgnoreCase);

        public static string RegexImage(string value)
        {
            return String.IsNullOrEmpty(value) ? 
                       String.Empty : 
                       Regex_Image.Match(value).Groups["imgUrl"].Value;
        }

        /// <summary>
        /// 将指定字符串中的汉字转换为拼音首字母的缩写，其中非汉字保留为原字符
        /// </summary>
        public static string SpellFirst(string text)
        {
            char pinyin;
            byte[] array;
            StringBuilder sb = new StringBuilder();
            foreach (char c in text)
            {
                pinyin = c;
                array = Encoding.Default.GetBytes(new char[] { c });

                if (array.Length == 2)
                {
                    int i = array[0] * 0x100 + array[1];

                    if (i < 0xB0A1) pinyin = c;
                    else
                        if (i < 0xB0C5) pinyin = 'a';
                        else
                            if (i < 0xB2C1) pinyin = 'b';
                            else
                                if (i < 0xB4EE) pinyin = 'c';
                                else
                                    if (i < 0xB6EA) pinyin = 'd';
                                    else
                                        if (i < 0xB7A2) pinyin = 'e';
                                        else
                                            if (i < 0xB8C1) pinyin = 'f';
                                            else
                                                if (i < 0xB9FE) pinyin = 'g';
                                                else
                                                    if (i < 0xBBF7) pinyin = 'h';
                                                    else
                                                        if (i < 0xBFA6) pinyin = 'g';
                                                        else
                                                            if (i < 0xC0AC) pinyin = 'k';
                                                            else
                                                                if (i < 0xC2E8) pinyin = 'l';
                                                                else
                                                                    if (i < 0xC4C3) pinyin = 'm';
                                                                    else
                                                                        if (i < 0xC5B6) pinyin = 'n';
                                                                        else
                                                                            if (i < 0xC5BE) pinyin = 'o';
                                                                            else
                                                                                if (i < 0xC6DA) pinyin = 'p';
                                                                                else
                                                                                    if (i < 0xC8BB) pinyin = 'q';
                                                                                    else
                                                                                        if (i < 0xC8F6) pinyin = 'r';
                                                                                        else
                                                                                            if (i < 0xCBFA) pinyin = 's';
                                                                                            else
                                                                                                if (i < 0xCDDA) pinyin = 't';
                                                                                                else
                                                                                                    if (i < 0xCEF4) pinyin = 'w';
                                                                                                    else
                                                                                                        if (i < 0xD1B9) pinyin = 'x';
                                                                                                        else
                                                                                                            if (i < 0xD4D1) pinyin = 'y';
                                                                                                            else
                                                                                                                if (i < 0xD7FA) pinyin = 'z';
                }

                sb.Append(pinyin);
            }
            return sb.ToString();
        }
    }
}
